package ciyuan.Gui.Screen;

import ciyuan.MainClass;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.gui.screen.MainMenuScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SharedConstants;
import net.minecraft.util.Util;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class MainScreen {

    @SubscribeEvent
    public static void onGuiOpened(GuiOpenEvent event) {
        if (event.getGui() instanceof MainMenuScreen && !(event.getGui() instanceof MyMainMenu)) {
            event.setGui(new MyMainMenu());
        }
    }

    public static class MyMainMenu extends MainMenuScreen {

        private static final ResourceLocation MINECRAFT_TITLE_TEXTURES = new ResourceLocation("textures/gui/title/minecraft.png");

        @Override
        public void render(MatrixStack stack, int mouseX, int mouseY, float partialTicks) {
            stack.pushPose();
            renderBack(stack);
            this.minecraft.getTextureManager().bind(MINECRAFT_TITLE_TEXTURES);
            this.blit(stack, this.width / 2 - 274 / 2, 30, 0, 0, 155, 44);
            this.blit(stack, this.width / 2 - 274 / 2 + 155, 30, 0, 45, 155, 44);

            stack.pushPose();
            stack.translate((float) (this.width / 2 + 90), 70.0F, 0.0F);
            stack.mulPose(Vector3f.ZP.rotationDegrees(-22.5F));
            float f2 = 1.8F - MathHelper.abs(MathHelper.sin((float) (Util.getMillis() % 1000L) / 1000.0F * ((float) Math.PI * 2F)) * 0.1F);
            f2 = f2 * 100.0F / (float) (this.font.width("繁星次元") + 32);
            stack.scale(f2, f2, f2);
            drawCenteredString(stack, this.font, "繁星次元", 0, -8, 16776960);
            stack.popPose();
            //render version
            String s = "Minecraft " + SharedConstants.getCurrentVersion().getName() + ("release".equalsIgnoreCase(this.minecraft.getVersionType()) ? "" : "/" + this.minecraft.getVersionType());
            if (this.minecraft.isProbablyModded()) {
                s = s + I18n.get("menu.modded");
            }
            ForgeHooksClient.renderMainMenu(this, stack, this.font, this.width, this.height);
            String s1 = "Copyright Mojang AB. Do not distribute!";
            drawString(stack, this.font, s1, this.width - this.font.width(s1) - 2, this.height - 10, 0xFFFFFFFF);
            // render button
            for (net.minecraft.client.gui.widget.Widget button : this.buttons) {
                button.render(stack, mouseX, mouseY, minecraft.getFrameTime());
            }
            stack.popPose();
        }

        private static int time = 0;
        private static final List<ResourceLocation> BACKGROUND_RESOURCE_LIST = new ArrayList<>();
        private static int i = 0;

        static {
            for (int i = 0; i <= 60; i++) {
                BACKGROUND_RESOURCE_LIST.add(new ResourceLocation(MainClass.MODID, "textures/gui/mainmenu/b" + i + ".png"));
            }
        }

        private void renderBack(MatrixStack stack) {
            if (time++ > 5) {
                if (++i > 60) i = 0;
                this.minecraft.getTextureManager().bind(BACKGROUND_RESOURCE_LIST.get(i));
                blit(stack, 0, 0, this.width, this.height, 0.0F, 0.0F, 16, 128, 16, 128);
                time = 0;
            }
        }

    }




}
